﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace 城市天然气管道风险管理与决策系统
{
    public partial class Form_menu_1_3 : Form
    {
        public Form_menu_1_3()
        {
            InitializeComponent();
        }

        private void Form_menu_1_3_Load(object sender, EventArgs e)
        {
            textBox1.ReadOnly = true;
            textBox2.ReadOnly = true;
            textBox3.ReadOnly = true;
            textBox4.ReadOnly = true;

            textBox1.Text = GlobalData.default_pipeline.Id.ToString();
            textBox2.Text = GlobalData.default_pipeline.Name;
            textBox4.Text = GlobalData.default_pipeline.StartLocation;
            textBox3.Text = GlobalData.default_pipeline.EndLocation;

            if(GlobalData.default_pipeline.Fixs != null)
            {
                foreach (var date in GlobalData.default_pipeline.Fixs)
                {
                    listBox1.Items.Add(date.FixDate.ToString());
                }
            }
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime date = dateTimePicker1.Value.Date;
            string level = comboBox1.Text;
            string unit = textBox6.Text;
            string person = richTextBox1.Text;
            string reason = richTextBox2.Text;
            string reamrk = richTextBox3.Text;
            string method = richTextBox4.Text;
            double price = 0;
            try
            {
                price = double.Parse(textBox7.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("输入有误");
                return;
            }
            FixInfo fix = new FixInfo(date, level, unit, price, person, reason, method, reamrk);
            if (GlobalData.default_pipeline.Fixs == null) GlobalData.default_pipeline.Fixs = new List<FixInfo> { fix };
            else GlobalData.default_pipeline.Fixs.Add(fix);
            listBox1.Items.Add(date.ToString());
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
                return;
            DateTime date = dateTimePicker1.Value.Date;
            string level = comboBox1.Text;
            string unit = textBox6.Text;
            double price = double.Parse(textBox7.Text);
            string person = richTextBox1.Text;
            string reason = richTextBox2.Text;
            string reamrk = richTextBox3.Text;
            string method = richTextBox4.Text;
            FixInfo fix = new FixInfo(date, level, unit, price, person, reason, method, reamrk);
            int index = listBox1.SelectedIndex;
            GlobalData.default_pipeline.Fixs[index] = fix;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            GlobalData.default_pipeline.Fixs.RemoveAt(index);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
                return;
            else
            {
                int index = listBox1.SelectedIndex;
                var fix = GlobalData.default_pipeline.Fixs[index];
                dateTimePicker1.Value = fix.FixDate;
                comboBox1.Text = fix.FixLevel;
                textBox6.Text = fix.FixUnit;
                textBox7.Text = fix.FixPrice.ToString();
                richTextBox1.Text = fix.FixPerson;
                richTextBox2.Text = fix.FixReason;
                richTextBox3.Text = fix.Remark;
                richTextBox4.Text = fix.Method;
            }
        }

        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            string print_info = "";
            print_info += "管道ID：" + GlobalData.default_pipeline.Id.ToString();
            print_info += "\n管道名称：" + GlobalData.default_pipeline.Name;
            print_info += "\n起始位置：" + GlobalData.default_pipeline.StartLocation;
            print_info += "\n末端位置：" + GlobalData.default_pipeline.EndLocation;
            print_info += "\n维修日期：" + dateTimePicker1.Value.Date.ToString();
            print_info += "\n维修级别：" + comboBox1.Text;
            print_info += "\n负责单位：" + textBox6.Text;
            print_info += "\n维修花费（万元）：" + textBox7.Text;
            print_info += "\n参与人员：" + richTextBox1.Text;
            print_info += "\n维修原因：" + richTextBox2.Text;
            print_info += "\n维修方法及效果：" + richTextBox4.Text;
            print_info += "\n备注：" + richTextBox3.Text;

            printDialog1.ShowDialog();
            e.Graphics.DrawString(print_info, richTextBox1.Font, Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
            printDocument1.DocumentName = GlobalData.default_pipeline.Name + "-管道信息";
        }
    }
}
